[% setvar title Stronger typing through tie. %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Stronger typing through tie.</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Michael Fowler &lt;<a href='mailto:michael@shoebox.net'>michael@shoebox.net</a>&gt;
  Date: 2 Aug 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 15
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Strong typing of variables can be implemented through the already-existent
tie mechanism.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Several people have requested strong typing as a feature, but have been shot
down with reasons such as &quot;it's un-Perl-like&quot;, with an added &quot;it'll slow
everything down for those who don't want it&quot;.  However, I think it can be
done in a fairly simple way through the use of the tie mechanism, which
makes the feature optional to those who want an even stricter environment.</p>
<p>Through a pragma or core module one gets various subroutines that produce
tied variables that verify they're only set to allowable values.
Unfortunately, accessing and manipulating tied variables is incredibly slow,
so improving their speed is a large part of making this proposal feasible.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>I envision syntax like this:</p>
<pre>    use typing;  # place your fingers on the home row..

    integer $foo, $bar, $blah;
    string($baz, $qux) = (&quot;bazarific&quot;, &quot;quxaroni&quot;);

    # and even..

    my integer $quux = 4;</pre>
<p>integer and string are subroutines automatically exported by typing.pm; they
simply tie their arguments to an appropriate class.</p>
<p>The last is a little questionable; it would require some magic on perl's
part to invoke some method or constructor for initializing <code>$quux</code>.  A
suggested alternative is something like <code>my $quux : integer</code>, but it
requires some manipulation on perl's part as well.</p>
<p>There is a drawback: exceptions raised (&quot;That's not an integer!&quot; *croak*)
are made at run-time, rather than compile-time (as would be more useful).
As a result you have to test all code paths if you are to use this as any
sort of internal sanity checking; I assume most everyone does this, but it
would be useful for it to be a compile-time check, so that perl -c would
catch all of your errors. This could possibly be accomplished by allowing
arguments to the pragma or variable initialization.</p>
<pre>    use typing qw(very-strict);
    my integer $foo : very-strict = 4;</pre>
<p>Which would enforce that you can only assign integer constants to $foo
(which are seen at compile-time), or other similarly declared integers (or
possibly promoted floats, chars, etc. if you wanted to get C-like).  This,
then, could allow you to do checking at compile-time.</p>
<p>Which then raises a few more problems (whew): how do you coax user input
(which is an SV) into a value $foo can accept with very-strict on?; what
happens when an external function (say, from a module) is being very-strict,
but is passed arguments from code that doesn't do type checking?</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>perldoc perltie</p>
</div>
